<?php

namespace common\models;

use common\helpers\ArrayHelper;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;

/**
 * This is the model class for table "tab".
 * 数据域实体类
 *
 * @author Bran <lin.yang@ht-med.com>
 * @copyright (c) 2015, HT-MED
 * @package common.models
 * @since version 1.0
 *
 * @property integer $id
 * @property integer $project_id
 * @property string $tab_name
 * @property string $tab_content
 * @property string $visits
 * @property string $single
 * @property integer $version_id
 * @property string $enter_note
 * @property integer $created_at
 * @property integer $updated_at
 */
class Tab extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'tab';
    }

    //是否是单条记录类表
    public static function singleList()
    {
        return [
            'Y' => '是',
            'N' => '否'
        ];
    }

    //是否是单条记录类表
    public static function getVisitList($version_id = 0)
    {
        $visits = Visit::find()->where(['project_id' => Yii::$app->session->get('defaultPid'), 'version_id' => $version_id])->orderBy('visit_seq asc')->all();
        if (!$visits) {
            return [];
        }
        $arr = [];
        foreach ($visits as $k => $v) {
            $arr[$v->id] = $v->visit_name;
        }
        return $arr;
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['project_id', 'tab_name', 'tab_content', 'version_id', 'visits'], 'required'],
            [['project_id', 'created_at', 'updated_at'], 'integer'],
            [['visits', 'enter_note'], 'string'],
            [['tab_name', 'tab_content'], 'string', 'max' => 120],
            ['tab_name', 'match', 'pattern' => "/^[0-9a-zA-Z_-]+$/", 'message' => '{attribute}英文、数字、-、_'],
            [['single'], 'string', 'max' => 1],

        ];
    }

    //新增是判断项目id和数据域名称不可重复
    public function uniqueTab()
    {
        return self::find()->where(['project_id' => $this->project_id, 'tab_name' => trim($this->tab_name), 'version_id' => $this->version_id])->exists();
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'project_id' => '项目编号',
            'tab_name' => '数据域名称(E)',
            'tab_content' => '数据域标签',
            'visits' => '访视',
            'single' => '是否单条记录',
            'enter_note' => '填写说明',
            'version_id' => '版本号ID',
        ];
    }

    public function fields()
    {
        return [
            'domain' => function ($data) {
                return $data->tab_content;
            },
            'single',
            'updated_at',
        ];
    }

    public function getVer()
    {
        return $this->hasOne(DbVersion::className(), ['id' => 'version_id']);
    }

    /**
     * 判断数据域是否被绑定
     * @param $tab_name
     */
    public static function getTabSign($tab_name)
    {
        $res = DmLabTabVarb::findOne([
            'project_id' => getSession('defaultPid'),
            'tab_name' => $tab_name,
        ]);

        if (!$res) {
            return false;
        }
        return true;
    }

    public function getVisitInfo()
    {
        $visits = ArrayHelper::map(Visit::find()->where(['project_id' => $this->project_id])->orderBy("visit_seq ASC")->all(), 'id', 'visit_name');
        $nowVisit = explode(',', $this->visits);
        $visitName = [];
        foreach ($nowVisit as $val) {
            if (!isset($visits[$val])) {
                continue;
            }
            $visitName[] = $visits[$val];
        }
        return implode('<br/>', $visitName);
    }
}
